package cn.edu.zut.salarymanagesystem.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

import cn.edu.zut.salarymanagesystem.R;
import cn.edu.zut.salarymanagesystem.bean.pojo.WorkRecord;

public class WorkRecordAdapter extends BaseAdapter {
    private final static ColorDrawable whiteColor = new ColorDrawable(Color.WHITE);
    private final static ColorDrawable grayColor = new ColorDrawable(Color.parseColor("#BEBEC1"));
    public List<WorkRecord> workRecords;
    public final SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");
    private final LayoutInflater inflater;
    private final Context context;

    public WorkRecordAdapter(Context context, List<WorkRecord> workRecords) {
        this.workRecords = workRecords;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
        Collections.sort(this.workRecords);
    }

    public List<WorkRecord> getWorkRecords() {
        return workRecords;
    }

    public void setWorkRecords(List<WorkRecord> workRecords) {
        this.workRecords = workRecords;
    }


    @Override
    public int getCount() {
        return workRecords == null ? 0 : workRecords.size();
    }

    @Override
    public Object getItem(int position) {
        return workRecords.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.pager_item, null);
            synchronized (this) {
                convertView.setBackground(position % 2 == 0 ? grayColor : whiteColor);
            }
            viewHolder.txtDate = convertView.findViewById(R.id.text_date);
            viewHolder.txtWorkprice = convertView.findViewById(R.id.text_price);
            viewHolder.txtNum = convertView.findViewById(R.id.text_num);
            viewHolder.itemName = convertView.findViewById(R.id.text_item_name);
            viewHolder.txtValid = convertView.findViewById(R.id.text_valid);
            viewHolder.txtCommentary = convertView.findViewById(R.id.text_commentary);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        WorkRecord wr = workRecords.get(position);
        viewHolder.txtDate.setText(sdf.format((wr.getTime())));
        viewHolder.txtWorkprice.setText(String.valueOf(wr.getValue()));
        //兼容Append类型
        viewHolder.itemName.setText(wr.getObjectName() == null ? wr.getCommentary() : wr.getObjectName());
        //处理commentary
        if (wr == null) {
            viewHolder.txtCommentary.setText("无");
        } else if (wr.getCommentary() == null) {
            viewHolder.txtCommentary.setText("无");
        } else if (wr.getCommentary().equals("")) {
            viewHolder.txtCommentary.setText("无");
        } else {
            viewHolder.txtCommentary.setText(wr.getCommentary());
        }
        viewHolder.txtNum.setText(String.valueOf(wr.getNumber()));
        if (wr.getValid() == 1) {
            viewHolder.txtValid.setText("是");
            viewHolder.txtValid.setTextColor(Color.GREEN);
        } else {
            viewHolder.txtValid.setText("否");
            viewHolder.txtValid.setTextColor(Color.RED);
        }
        return convertView;

    }

    @Override
    public void notifyDataSetChanged() {
        Collections.sort(this.workRecords);
        super.notifyDataSetChanged();
    }

    private static class ViewHolder {
        public TextView txtDate;
        public TextView itemName;
        public TextView txtCommentary;
        public TextView txtNum;
        public TextView txtWorkprice;
        public TextView txtValid;
    }
}


